﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace NetPE
{
    public partial class ReadonlyCollectionEditor<T> : BaseEditor where T : class
    {
        private class CollectionLinkingBindingList<TMember> : BindingList<TMember>
        {
            IList<TMember> collection;
            public CollectionLinkingBindingList(IList<TMember> coll) : base(coll) { collection = coll; }

            protected override void ClearItems()
            {
                collection.Clear();
                base.ClearItems();
            }
            protected override void InsertItem(int index, TMember item)
            {
                collection.Insert(index, item);
                base.InsertItem(index, item);
            }
            protected override void RemoveItem(int index)
            {
                collection.RemoveAt(index);
                base.RemoveItem(index);
            }
            protected override void SetItem(int index, TMember item)
            {
                collection[index] = item;
                base.SetItem(index, item);
            }
        }

        string n;
        public string PropertyName { get { return n; } set { n = value; } }

        private SplitContainer can;
        private ListBox sects;
        private PropertyEditor prop;

        List<PropertyAction> acts = new List<PropertyAction>();
        public List<PropertyAction> Actions { get { return acts; } }

        public ReadonlyCollectionEditor()
        {
            InitializeComponent();

            sects.SelectedIndexChanged += new EventHandler(ItemChanged);
            prop.PropertyChanged += new EventHandler(RefreshText);
        }

        void InitializeComponent()
        {
            can = new SplitContainer();
            sects = new ListBox();
            prop = new PropertyEditor();
            can.Panel1.SuspendLayout();
            can.Panel2.SuspendLayout();
            can.SuspendLayout();
            this.SuspendLayout();

            can.BackColor = Color.White;
            can.Dock = DockStyle.Fill;
            can.Location = new Point(0, 0);

            can.Panel1.Controls.Add(this.sects);

            can.Panel2.BackColor = Color.Black;
            can.Panel2.Controls.Add(this.prop);
            can.Size = new Size(461, 389);
            can.SplitterDistance = 100;
            can.TabIndex = 0;

            sects.BackColor = Color.Black;
            sects.BorderStyle = BorderStyle.None;
            sects.Dock = DockStyle.Fill;
            sects.ForeColor = Color.White;
            sects.FormattingEnabled = true;
            sects.IntegralHeight = false;
            sects.Location = new Point(0, 0);
            sects.Size = new Size(100, 389);
            sects.TabIndex = 0;

            prop.AutoScroll = true;
            prop.BackgroundImageLayout = ImageLayout.None;
            prop.Dock = DockStyle.Fill;
            prop.Font = new Font("Arial", 9F);
            prop.Location = new Point(0, 0);
            prop.Object = null;
            prop.Size = new Size(357, 389);
            prop.TabIndex = 0;

            Controls.Add(can);
            this.Size = new Size(461, 389);
            can.Panel1.ResumeLayout(false);
            can.Panel2.ResumeLayout(false);
            can.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        protected override void RefreshData()
        {
            CollectionLinkingBindingList<T> bList = new CollectionLinkingBindingList<T>(Object as IList<T>);
            sects.DisplayMember = n;
            sects.DataSource = bList;
            prop.Object = null;
            prop.Visible = false;
        }

        private void ItemChanged(object sender, EventArgs e)
        {
            if (sects.SelectedItem != null)
            {
                prop.Actions.Clear();
                prop.Actions.AddRange(acts);
                prop.Visible = false;
                prop.Object = sects.SelectedItem as T;
                prop.Visible = true;
            }
        }

        private void RefreshText(object sender, EventArgs e)
        {
            (sects.DataSource as BindingList<T>).ResetBindings();
        }
    }
}
